package no.weather;

import java.util.Calendar;
import java.util.TimeZone;

import no.weather.weatherProvider.WeatherDatabase;
import no.weather.weatherProvider.WeatherProvider;
import no.weather.widget.WidgetProvider;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Weather extends Activity {
	private static final String LOG_ID = "no.weather.Weather";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		final WeatherProvider provider = new WeatherProvider(
				getApplicationContext());

		Button getStationsButton = (Button) findViewById(R.id.getStations);
		getStationsButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				provider.updateStations();
			}
		});
		Button removeStationsButton = (Button) findViewById(R.id.removeStations);
		removeStationsButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				provider.resetStationDb();
			}
		});

		Button getWeatherButton = (Button) findViewById(R.id.getWeather);
		getWeatherButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				openWeatherReport(provider.getNearestStation());

			}
		});

		Button showStationsButton = (Button) findViewById(R.id.showStations);
		showStationsButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				openStationView(false);

			}
		});

		Button showFavoriteStationButton = (Button) findViewById(R.id.showFavoriteStations);
		showFavoriteStationButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				openStationView(true);

			}
		});

		Button mapButton = (Button) findViewById(R.id.showMap);
		mapButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Weather.this, StationMap.class);
				startActivity(intent);
			}
		});

		Button widgetUpdateButton = (Button) findViewById(R.id.updateWidget);
		widgetUpdateButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				updateWidget();
			}
		});

	}

	private void openStationView(Boolean showFavoritesOnly) {
		Intent intent = new Intent(this, WeatherStationsView.class);
		intent.putExtra(WeatherStationsView.SHOW_FAVORITE_ONLY,
				showFavoritesOnly);
		startActivity(intent);
	}

	private void openWeatherReport(WeatherStation station) {

		Intent intent = new Intent(this, WeatherReportView.class);
		intent.putExtra(WeatherDatabase.STATION_ID, station.getId());
		intent.putExtra(WeatherDatabase.STATION_PROVIDER, station.getProvider());
		startActivity(intent);
	}

	private void updateWidget() {
		// AlarmManager alarm = (AlarmManager)
		// getSystemService(Context.ALARM_SERVICE);
		Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
		int minutes = time.get(Calendar.MINUTE);
		time.add(Calendar.MINUTE, 60 - minutes);
		Intent intent = new Intent();
		intent.setAction(WidgetProvider.UPDATE_WEATHER_ACTION);
		this.sendBroadcast(intent);
		Log.v(LOG_ID, "Broadcast sent for updating widget");
	}

}
